using Kopernikus.Collections.Generic;
using Kopernikus.Parsing.Utility;

namespace Kopernikus.Parsing.GrammarSets
{
    internal class FollowSets
    {
        private readonly IGrammar m_grammar;
        private SetMap<Symbol, Symbol> m_followSets;

        public FollowSets(IGrammar grammar)
        {
            m_grammar = grammar;
        }


        public IGrammar Grammar
        {
            get { return m_grammar; }
        }

        public Set<Symbol> GetFollow(Symbol s)
        {
            Calculate();
            return m_followSets.GetSet(s);
        }

        private void Calculate()
        {
            //if (m_followSets == null)
            //{
            //    m_followSets = new SetMap<Symbol, Symbol>();
            //    foreach (Symbol nonTerminalSymbol in Grammar.SymbolSets.TerminalSymbols)
            //    {
            //        m_followSets.MakeSet(nonTerminalSymbol);
            //    }

            //    bool added = true;
            //    while (added)
            //    {
            //        foreach (Production p in Grammar.Productions)
            //        {
            //            foreach (Symbol s in p.RHS)
            //            {

            //                added = m_followSets.MakeSet(p.LHS).Add(m_followSets.MakeSet(s));

            //                if (!Grammar.SymbolSets.NullableSymbols.Contains(s))
            //                {
            //                    break;
            //                }
            //            }
            //        }
            //    }
            //}
        }
    }
}